package com.e1xp.deport_management_system.controller;


import com.e1xp.deport_management_system.entity.Order;
import com.e1xp.deport_management_system.entity.User;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.service.IOrderService;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import com.e1xp.deport_management_system.utils.OrderReceive;
import com.e1xp.deport_management_system.utils.PickWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    IOrderService orderService;

    /**
     * 用于渲染订单陈列页面
     *
     * @param instanceId 实例id
     * @return ModelAndView
     */
    @RequestMapping("/orderData")
    public ModelAndView orderData(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orderList = orderService.getList(instanceId);
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("orderManager");//设置视图名
        return modelAndView;
    }

    /**
     * 用于添加订单
     *
     * @param instanceId   实例id
     * @param orderReceive 订单接收实体
     * @return AjaxResponse
     * @throws CustomException 当找不到该实例，订单信息不完整，商品代码不存在时抛出该异常
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResponse addOrder(@SessionAttribute long instanceId, @RequestBody OrderReceive orderReceive) throws CustomException {
        orderService.add(instanceId, orderReceive.getOrder(), orderReceive.getOrderProductList());
        return AjaxResponse.success();
    }

    /**
     * 渲染添加订单页面
     *
     * @return ModelAndView
     */
    @RequestMapping("/addOrderPage")
    public ModelAndView addOrderPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addOrder");
        return modelAndView;
    }

    /**
     * 用于渲染详情页面
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return ModelAndView
     */
    @RequestMapping("/content")
    public ModelAndView content(@SessionAttribute long instanceId, long orderId) {
        ModelAndView modelAndView = new ModelAndView();
        Order order = orderService.get(instanceId, orderId);
        if (order == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.addObject("order", order);
            List<Map<String, String>> orderProductList = orderService.getOrderProductList(instanceId, orderId);
            modelAndView.addObject("orderProductList", orderProductList);
            modelAndView.setViewName("orderProductManager");
        }
        return modelAndView;
    }

    /**
     * 将订单转为拣货
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return AjaxResponse
     */
    @PostMapping("/undertake")
    @ResponseBody
    @Transactional
    public AjaxResponse undertake(@SessionAttribute long instanceId, @SessionAttribute User user, long orderId) throws CustomException {
        orderService.pick(instanceId, orderId, user.getId());
        return AjaxResponse.success();
    }

    /**
     * 渲染订单详情页面
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return ModelAndView
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@SessionAttribute long instanceId, int orderId) {
        ModelAndView modelAndView = new ModelAndView();
        Order order = orderService.get(instanceId, orderId);
        if (order == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.addObject("order", order);
            modelAndView.setViewName("changeOrder");
        }
        return modelAndView;
    }

    /**
     * 用于更新订单
     *
     * @param instanceId 实例id
     * @param order      订单实体
     * @return AjaxResponse
     * @throws CustomException 当订单数据不完整时，抛出该异常
     */
    @PostMapping("/update")
    @ResponseBody
    @Transactional
    public AjaxResponse update(@SessionAttribute long instanceId, Order order) throws CustomException {
        orderService.updateOrder(instanceId, order);
        return AjaxResponse.success();
    }

    /**
     * 用于渲染我的拣货订单
     *
     * @param instanceId 实例id
     * @param user       用户类实体
     * @return ModelAndView
     */
    @RequestMapping("/myPickingOrderPage")
    public ModelAndView pickingOrderPage(@SessionAttribute long instanceId, @SessionAttribute User user) {
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orderList = orderService.getPickingOrder(instanceId, user.getId());
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("myPickingOrderManager");
        return modelAndView;
    }

    /**
     * 用于渲染拣货页面
     *
     * @param orderId 订单id
     * @return ModelAndView
     */
    @RequestMapping("/pickProductContent")
    public ModelAndView pickProductPage(long orderId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, String>> toPickProductList = orderService.toPickProductList(orderId);
        modelAndView.addObject("orderProductList", toPickProductList);
        modelAndView.addObject("orderId", orderId);
        modelAndView.setViewName("pickProduct");
        return modelAndView;
    }

    /**
     * 用于拣货
     *
     * @param instanceId  实例id
     * @param user        用户实体类
     * @param pickWrapper 拣货Wrapper
     * @return AjaxResponse
     */
    @PostMapping("/pickProduct")
    @ResponseBody
    public AjaxResponse pickProduct(@SessionAttribute long instanceId, @SessionAttribute User user, @RequestBody PickWrapper pickWrapper) throws CustomException {
        orderService.pickProduct(instanceId, user.getId(), pickWrapper);
        return AjaxResponse.success();
    }

    /**
     * 用于渲染待处理订单页面
     *
     * @param instanceId 实例id
     * @return ModelAndView
     */
    @RequestMapping("/pendingOrderPage")
    public ModelAndView pendingOrderPage(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orderList = orderService.getPendingOrderList(instanceId);
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("pendingOrderManager");
        return modelAndView;
    }

    /**
     * 用于获取拣货列表商品与货架信息
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return AjaxResponse
     */
    @PostMapping("/getPickingData")
    @ResponseBody
    public AjaxResponse getPickingData(@SessionAttribute long instanceId, long orderId) {
        Map<String, Object> resultMap = orderService.getPickingData(instanceId, orderId);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 用于渲染待发货页面
     *
     * @param instanceId 实例id
     * @return ModelAndView
     */
    @RequestMapping("/myWaitDeliverOrderPage")
    public ModelAndView myWaitDeliverOrderPage(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orderList = orderService.getWaitOrder(instanceId);
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("myWaitDeliverOrderManager");
        return modelAndView;
    }

    /**
     * 用于发货
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return AjaxResponse
     * @throws CustomException 当该订单不存在，订单状态不满足时抛出该异常
     */
    @PostMapping("/sendGoods")
    @ResponseBody
    public AjaxResponse sendGoods(@SessionAttribute long instanceId, long orderId) throws CustomException {
        orderService.sendGoods(instanceId, orderId);
        return AjaxResponse.success();
    }

    /**
     * 用于删除订单
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return AjaxResponse
     * @throws CustomException 当订单不存在，订单已进入拣货时抛出该异常
     */
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResponse delete(@SessionAttribute long instanceId, long orderId) throws CustomException {
        orderService.delete(instanceId, orderId);
        return AjaxResponse.success();
    }
}
